package com.gpnu.crm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gpnu.crm.domain.Role;
import com.gpnu.crm.query.QueryObject;
import com.gpnu.crm.service.IPermissionService;
import com.gpnu.crm.service.IRoleService;
import com.gpnu.crm.util.JSONResult;

@Controller
@RequestMapping({ "role" })
public class RoleController {
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IPermissionService permissionService;

	@RequestMapping({ "list" })
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		model.addAttribute("pageInfo", this.roleService.query(qo));
		return "role/list";
	}

	@RequestMapping({ "input" })
	public String input(Long id, Model model) {
		if (id != null) {
			model.addAttribute("entity", this.roleService.get(id));
			model.addAttribute("selected", this.permissionService.listByRoleId(id));
			model.addAttribute("notSelected", this.permissionService.listNotByRoleId(id));
		} else {
			model.addAttribute("notSelected", this.permissionService.list());
		}
		return "role/input";
	}

	@RequestMapping({ "saveOrUpdate" })
	public String saveOrUpdate(Role entity, Long[] ids) {
		this.roleService.saveOrUpdate(entity, ids);
		return "redirect:/role/list.do";
	}

	@RequestMapping({ "delete" })
	@ResponseBody
	public Object delete(Long id) {
		if (id != null) {
			this.roleService.delete(id);
		}
		return new JSONResult();
	}
}
